package com.dbh.company.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelWriter;

import java.util.ArrayList;
import java.util.List;

public class ExcelUtil {

    public static void main(String[] args) {
        String jsonStr = FileUtil.readString("D:\\data\\1.txt", "UTF-8");
        JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
        Object jsonObjectData = jsonObject.get("data");
        Object o = JSONUtil.parseObj(jsonObjectData).get("list");
        JSONArray objects = JSONUtil.parseArray(o);
        ExcelWriter writer = cn.hutool.poi.excel.ExcelUtil.getBigWriter("D:\\data\\1.xlsx");
        writer.renameSheet("数据");
        List<String> headList = new ArrayList<>();
        headList.add("id");
        headList.add("resblockId");
        headList.add("resblockName");
        headList.add("cityRegion");
        headList.add("auditStateStr");
        headList.add("fileName");
        headList.add("fileFormat");
        headList.add("cuserName");
        headList.add("ctime");
        writer.setColumnWidth(0, 20);
        writer.setColumnWidth(1, 20);
        writer.setColumnWidth(2, 20);
        writer.setColumnWidth(3, 20);
        writer.setColumnWidth(4, 20);
        writer.setColumnWidth(5, 20);
        writer.setColumnWidth(6, 20);
        writer.setColumnWidth(7, 20);
        writer.setColumnWidth(8, 20);
        writer.setColumnWidth(9, 20);
        // 写标题
        writer.writeSecHeadRow(headList);
        List<List<Object>> rowList = new ArrayList<>();
        for (Object item : objects) {
            List<Object> cellList = new ArrayList<>();
            JSONObject jsonObject1 = JSONUtil.parseObj(item);
            cellList.add(jsonObject1.get("id"));
            cellList.add(jsonObject1.get("resblockId"));
            cellList.add(jsonObject1.get("resblockName"));
            cellList.add(jsonObject1.get("cityRegion"));
            cellList.add(jsonObject1.get("auditStateStr"));
            cellList.add(jsonObject1.get("fileName"));
            cellList.add(jsonObject1.get("fileFormat"));
            cellList.add(jsonObject1.get("cuserName"));
            cellList.add(jsonObject1.get("ctime"));
            rowList.add(cellList);
        }
        writer.write(rowList, true);
        writer.close();
    }
}
